﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace emulib
{
    public class DatabaseManager
    {
        IMmoDatabase dbHandle;
        public Boolean connectResult
        {
            get; private set;
        }
        public DatabaseManager(IMmoServ ims)
        {
            switch (ims.GetInfo().dbInfo.dbType)
            {
                case DatabaseType.MySQL:
                    dbHandle = new MySQLHandler();
                    break;
                case DatabaseType.MSSQL:
                    dbHandle = new MSSQLHandler();
                    break;
                case DatabaseType.MSSQLCE:
                    dbHandle = new MSSQLCEHandler();
                    break;
                case DatabaseType.SQLite:
                    dbHandle = new SQLiteHandler();
                    break;
                default:
                    throw new Exception("Unable to determine database type.");
            }
            connectResult = dbHandle.Connect(ims.GetInfo());
            if (!connectResult)
            {
                throw new Exception("Connection to database failed.");
            }
        }

        /// <summary>
        /// Alias of dbHandle.Disconnect and Disconnect()
        /// </summary>
        public void Close() { if(connectResult) dbHandle.Disconnect(); }

        /// <summary>
        /// Disconnect from database
        /// </summary>
        public void Disconnect() { Close(); }

        ~DatabaseManager()
        {
            if(connectResult)
                dbHandle.Disconnect();
        }
    }
}
